{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "logical and\n",
      "epoch 0 sample 0  [1 2 0 0 0 0 0]\n",
      "epoch 0 sample 1  [1 2 0 1 -1 0 -1]\n",
      "epoch 0 sample 2  [0 2 -1 1 0 -1 -1]\n",
      "epoch 0 sample 3  [0 1 -2 0 1 1 1]\n",
      "epoch 1 sample 0  [1 2 -1 0 0 0 0]\n",
      "epoch 1 sample 1  [1 2 -1 0 0 0 0]\n",
      "epoch 1 sample 2  [1 2 -1 1 0 -1 -1]\n",
      "epoch 1 sample 3  [1 1 -2 0 1 1 1]\n",
      "epoch 2 sample 0  [2 2 -1 0 0 0 0]\n",
      "epoch 2 sample 1  [2 2 -1 1 -1 0 -1]\n",
      "epoch 2 sample 2  [1 2 -2 0 0 0 0]\n",
      "epoch 2 sample 3  [1 2 -2 1 0 0 0]\n",
      "epoch 3 sample 0  [1 2 -2 0 0 0 0]\n",
      "epoch 3 sample 1  [1 2 -2 0 0 0 0]\n",
      "epoch 3 sample 2  [1 2 -2 0 0 0 0]\n",
      "epoch 3 sample 3  [1 2 -2 1 0 0 0]\n",
      "epoch 4 sample 0  [1 2 -2 0 0 0 0]\n",
      "epoch 4 sample 1  [1 2 -2 0 0 0 0]\n",
      "epoch 4 sample 2  [1 2 -2 0 0 0 0]\n",
      "epoch 4 sample 3  [1 2 -2 1 0 0 0]\n",
      "epoch 5 sample 0  [1 2 -2 0 0 0 0]\n",
      "epoch 5 sample 1  [1 2 -2 0 0 0 0]\n",
      "epoch 5 sample 2  [1 2 -2 0 0 0 0]\n",
      "epoch 5 sample 3  [1 2 -2 1 0 0 0]\n",
      "epoch 6 sample 0  [1 2 -2 0 0 0 0]\n",
      "epoch 6 sample 1  [1 2 -2 0 0 0 0]\n",
      "epoch 6 sample 2  [1 2 -2 0 0 0 0]\n",
      "epoch 6 sample 3  [1 2 -2 1 0 0 0]\n",
      "epoch 7 sample 0  [1 2 -2 0 0 0 0]\n",
      "epoch 7 sample 1  [1 2 -2 0 0 0 0]\n",
      "epoch 7 sample 2  [1 2 -2 0 0 0 0]\n",
      "epoch 7 sample 3  [1 2 -2 1 0 0 0]\n",
      "epoch 8 sample 0  [1 2 -2 0 0 0 0]\n",
      "epoch 8 sample 1  [1 2 -2 0 0 0 0]\n",
      "epoch 8 sample 2  [1 2 -2 0 0 0 0]\n",
      "epoch 8 sample 3  [1 2 -2 1 0 0 0]\n",
      "epoch 9 sample 0  [1 2 -2 0 0 0 0]\n",
      "epoch 9 sample 1  [1 2 -2 0 0 0 0]\n",
      "epoch 9 sample 2  [1 2 -2 0 0 0 0]\n",
      "epoch 9 sample 3  [1 2 -2 1 0 0 0]\n",
      "logical or\n",
      "epoch 0 sample 0  [1 2 0 0 0 0 0]\n",
      "epoch 0 sample 1  [1 2 0 1 0 0 0]\n",
      "epoch 0 sample 2  [1 2 0 1 0 0 0]\n",
      "epoch 0 sample 3  [1 2 0 1 0 0 0]\n",
      "epoch 1 sample 0  [1 2 0 0 0 0 0]\n",
      "epoch 1 sample 1  [1 2 0 1 0 0 0]\n",
      "epoch 1 sample 2  [1 2 0 1 0 0 0]\n",
      "epoch 1 sample 3  [1 2 0 1 0 0 0]\n",
      "epoch 2 sample 0  [1 2 0 0 0 0 0]\n",
      "epoch 2 sample 1  [1 2 0 1 0 0 0]\n",
      "epoch 2 sample 2  [1 2 0 1 0 0 0]\n",
      "epoch 2 sample 3  [1 2 0 1 0 0 0]\n",
      "epoch 3 sample 0  [1 2 0 0 0 0 0]\n",
      "epoch 3 sample 1  [1 2 0 1 0 0 0]\n",
      "epoch 3 sample 2  [1 2 0 1 0 0 0]\n",
      "epoch 3 sample 3  [1 2 0 1 0 0 0]\n",
      "epoch 4 sample 0  [1 2 0 0 0 0 0]\n",
      "epoch 4 sample 1  [1 2 0 1 0 0 0]\n",
      "epoch 4 sample 2  [1 2 0 1 0 0 0]\n",
      "epoch 4 sample 3  [1 2 0 1 0 0 0]\n",
      "epoch 5 sample 0  [1 2 0 0 0 0 0]\n",
      "epoch 5 sample 1  [1 2 0 1 0 0 0]\n",
      "epoch 5 sample 2  [1 2 0 1 0 0 0]\n",
      "epoch 5 sample 3  [1 2 0 1 0 0 0]\n",
      "epoch 6 sample 0  [1 2 0 0 0 0 0]\n",
      "epoch 6 sample 1  [1 2 0 1 0 0 0]\n",
      "epoch 6 sample 2  [1 2 0 1 0 0 0]\n",
      "epoch 6 sample 3  [1 2 0 1 0 0 0]\n",
      "epoch 7 sample 0  [1 2 0 0 0 0 0]\n",
      "epoch 7 sample 1  [1 2 0 1 0 0 0]\n",
      "epoch 7 sample 2  [1 2 0 1 0 0 0]\n",
      "epoch 7 sample 3  [1 2 0 1 0 0 0]\n",
      "epoch 8 sample 0  [1 2 0 0 0 0 0]\n",
      "epoch 8 sample 1  [1 2 0 1 0 0 0]\n",
      "epoch 8 sample 2  [1 2 0 1 0 0 0]\n",
      "epoch 8 sample 3  [1 2 0 1 0 0 0]\n",
      "epoch 9 sample 0  [1 2 0 0 0 0 0]\n",
      "epoch 9 sample 1  [1 2 0 1 0 0 0]\n",
      "epoch 9 sample 2  [1 2 0 1 0 0 0]\n",
      "epoch 9 sample 3  [1 2 0 1 0 0 0]\n",
      "logical xor\n",
      "epoch 0 sample 0  [1 2 0 0 0 0 0]\n",
      "epoch 0 sample 1  [1 2 0 1 0 0 0]\n",
      "epoch 0 sample 2  [1 2 0 1 0 0 0]\n",
      "epoch 0 sample 3  [1 2 0 1 -1 -1 -1]\n",
      "epoch 1 sample 0  [0 1 -1 0 0 0 0]\n",
      "epoch 1 sample 1  [0 1 -1 0 1 0 1]\n",
      "epoch 1 sample 2  [1 1 0 1 0 0 0]\n",
      "epoch 1 sample 3  [1 1 0 1 -1 -1 -1]\n",
      "epoch 2 sample 0  [0 0 -1 0 0 0 0]\n",
      "epoch 2 sample 1  [0 0 -1 0 1 0 1]\n",
      "epoch 2 sample 2  [1 0 0 0 0 1 1]\n",
      "epoch 2 sample 3  [1 1 1 1 -1 -1 -1]\n",
      "epoch 3 sample 0  [0 0 0 0 0 0 0]\n",
      "epoch 3 sample 1  [0 0 0 0 1 0 1]\n",
      "epoch 3 sample 2  [1 0 1 1 0 0 0]\n",
      "epoch 3 sample 3  [1 0 1 1 -1 -1 -1]\n",
      "epoch 4 sample 0  [0 -1 0 0 0 0 0]\n",
      "epoch 4 sample 1  [0 -1 0 0 1 0 1]\n",
      "epoch 4 sample 2  [1 -1 1 0 0 1 1]\n",
      "epoch 4 sample 3  [1 0 2 1 -1 -1 -1]\n",
      "epoch 5 sample 0  [0 -1 1 1 0 0 -1]\n",
      "epoch 5 sample 1  [0 -1 0 0 1 0 1]\n",
      "epoch 5 sample 2  [1 -1 1 0 0 1 1]\n",
      "epoch 5 sample 3  [1 0 2 1 -1 -1 -1]\n",
      "epoch 6 sample 0  [0 -1 1 1 0 0 -1]\n",
      "epoch 6 sample 1  [0 -1 0 0 1 0 1]\n",
      "epoch 6 sample 2  [1 -1 1 0 0 1 1]\n",
      "epoch 6 sample 3  [1 0 2 1 -1 -1 -1]\n",
      "epoch 7 sample 0  [0 -1 1 1 0 0 -1]\n",
      "epoch 7 sample 1  [0 -1 0 0 1 0 1]\n",
      "epoch 7 sample 2  [1 -1 1 0 0 1 1]\n",
      "epoch 7 sample 3  [1 0 2 1 -1 -1 -1]\n",
      "epoch 8 sample 0  [0 -1 1 1 0 0 -1]\n",
      "epoch 8 sample 1  [0 -1 0 0 1 0 1]\n",
      "epoch 8 sample 2  [1 -1 1 0 0 1 1]\n",
      "epoch 8 sample 3  [1 0 2 1 -1 -1 -1]\n",
      "epoch 9 sample 0  [0 -1 1 1 0 0 -1]\n",
      "epoch 9 sample 1  [0 -1 0 0 1 0 1]\n",
      "epoch 9 sample 2  [1 -1 1 0 0 1 1]\n",
      "epoch 9 sample 3  [1 0 2 1 -1 -1 -1]\n"
     ]
    }
   ],
   "source": [
    "%run perceptron.py"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "logical and\n",
      "epoch 0 sample 0  [1 2 0]\n",
      "epoch 0 sample 1  [1 2 0]\n",
      "epoch 0 sample 2  [0 2 -1]\n",
      "epoch 0 sample 3  [0 1 -2]\n",
      "epoch 1 sample 0  [1 2 -1]\n",
      "epoch 1 sample 1  [1 2 -1]\n",
      "epoch 1 sample 2  [1 2 -1]\n",
      "epoch 1 sample 3  [1 1 -2]\n",
      "epoch 2 sample 0  [2 2 -1]\n",
      "epoch 2 sample 1  [2 2 -1]\n",
      "epoch 2 sample 2  [1 2 -2]\n",
      "epoch 2 sample 3  [1 2 -2]\n",
      "epoch 3 sample 0  [1 2 -2]\n",
      "epoch 3 sample 1  [1 2 -2]\n",
      "epoch 3 sample 2  [1 2 -2]\n",
      "epoch 3 sample 3  [1 2 -2]\n",
      "epoch 4 sample 0  [1 2 -2]\n",
      "epoch 4 sample 1  [1 2 -2]\n",
      "epoch 4 sample 2  [1 2 -2]\n",
      "epoch 4 sample 3  [1 2 -2]\n",
      "epoch 5 sample 0  [1 2 -2]\n",
      "epoch 5 sample 1  [1 2 -2]\n",
      "epoch 5 sample 2  [1 2 -2]\n",
      "epoch 5 sample 3  [1 2 -2]\n",
      "epoch 6 sample 0  [1 2 -2]\n",
      "epoch 6 sample 1  [1 2 -2]\n",
      "epoch 6 sample 2  [1 2 -2]\n",
      "epoch 6 sample 3  [1 2 -2]\n",
      "epoch 7 sample 0  [1 2 -2]\n",
      "epoch 7 sample 1  [1 2 -2]\n",
      "epoch 7 sample 2  [1 2 -2]\n",
      "epoch 7 sample 3  [1 2 -2]\n",
      "epoch 8 sample 0  [1 2 -2]\n",
      "epoch 8 sample 1  [1 2 -2]\n",
      "epoch 8 sample 2  [1 2 -2]\n",
      "epoch 8 sample 3  [1 2 -2]\n",
      "epoch 9 sample 0  [1 2 -2]\n",
      "epoch 9 sample 1  [1 2 -2]\n",
      "epoch 9 sample 2  [1 2 -2]\n",
      "epoch 9 sample 3  [1 2 -2]\n",
      "logical or\n",
      "epoch 0 sample 0  [1 2 0]\n",
      "epoch 0 sample 1  [1 2 0]\n",
      "epoch 0 sample 2  [1 2 0]\n",
      "epoch 0 sample 3  [1 2 0]\n",
      "epoch 1 sample 0  [1 2 0]\n",
      "epoch 1 sample 1  [1 2 0]\n",
      "epoch 1 sample 2  [1 2 0]\n",
      "epoch 1 sample 3  [1 2 0]\n",
      "epoch 2 sample 0  [1 2 0]\n",
      "epoch 2 sample 1  [1 2 0]\n",
      "epoch 2 sample 2  [1 2 0]\n",
      "epoch 2 sample 3  [1 2 0]\n",
      "epoch 3 sample 0  [1 2 0]\n",
      "epoch 3 sample 1  [1 2 0]\n",
      "epoch 3 sample 2  [1 2 0]\n",
      "epoch 3 sample 3  [1 2 0]\n",
      "epoch 4 sample 0  [1 2 0]\n",
      "epoch 4 sample 1  [1 2 0]\n",
      "epoch 4 sample 2  [1 2 0]\n",
      "epoch 4 sample 3  [1 2 0]\n",
      "epoch 5 sample 0  [1 2 0]\n",
      "epoch 5 sample 1  [1 2 0]\n",
      "epoch 5 sample 2  [1 2 0]\n",
      "epoch 5 sample 3  [1 2 0]\n",
      "epoch 6 sample 0  [1 2 0]\n",
      "epoch 6 sample 1  [1 2 0]\n",
      "epoch 6 sample 2  [1 2 0]\n",
      "epoch 6 sample 3  [1 2 0]\n",
      "epoch 7 sample 0  [1 2 0]\n",
      "epoch 7 sample 1  [1 2 0]\n",
      "epoch 7 sample 2  [1 2 0]\n",
      "epoch 7 sample 3  [1 2 0]\n",
      "epoch 8 sample 0  [1 2 0]\n",
      "epoch 8 sample 1  [1 2 0]\n",
      "epoch 8 sample 2  [1 2 0]\n",
      "epoch 8 sample 3  [1 2 0]\n",
      "epoch 9 sample 0  [1 2 0]\n",
      "epoch 9 sample 1  [1 2 0]\n",
      "epoch 9 sample 2  [1 2 0]\n",
      "epoch 9 sample 3  [1 2 0]\n",
      "logical xor\n",
      "epoch 0 sample 0  [1 2 0]\n",
      "epoch 0 sample 1  [1 2 0]\n",
      "epoch 0 sample 2  [1 2 0]\n",
      "epoch 0 sample 3  [1 2 0]\n",
      "epoch 1 sample 0  [0 1 -1]\n",
      "epoch 1 sample 1  [0 1 -1]\n",
      "epoch 1 sample 2  [1 1 0]\n",
      "epoch 1 sample 3  [1 1 0]\n",
      "epoch 2 sample 0  [0 0 -1]\n",
      "epoch 2 sample 1  [0 0 -1]\n",
      "epoch 2 sample 2  [1 0 0]\n",
      "epoch 2 sample 3  [1 1 1]\n",
      "epoch 3 sample 0  [0 0 0]\n",
      "epoch 3 sample 1  [0 0 0]\n",
      "epoch 3 sample 2  [1 0 1]\n",
      "epoch 3 sample 3  [1 0 1]\n",
      "epoch 4 sample 0  [0 -1 0]\n",
      "epoch 4 sample 1  [0 -1 0]\n",
      "epoch 4 sample 2  [1 -1 1]\n",
      "epoch 4 sample 3  [1 0 2]\n",
      "epoch 5 sample 0  [0 -1 1]\n",
      "epoch 5 sample 1  [0 -1 0]\n",
      "epoch 5 sample 2  [1 -1 1]\n",
      "epoch 5 sample 3  [1 0 2]\n",
      "epoch 6 sample 0  [0 -1 1]\n",
      "epoch 6 sample 1  [0 -1 0]\n",
      "epoch 6 sample 2  [1 -1 1]\n",
      "epoch 6 sample 3  [1 0 2]\n",
      "epoch 7 sample 0  [0 -1 1]\n",
      "epoch 7 sample 1  [0 -1 0]\n",
      "epoch 7 sample 2  [1 -1 1]\n",
      "epoch 7 sample 3  [1 0 2]\n",
      "epoch 8 sample 0  [0 -1 1]\n",
      "epoch 8 sample 1  [0 -1 0]\n",
      "epoch 8 sample 2  [1 -1 1]\n",
      "epoch 8 sample 3  [1 0 2]\n",
      "epoch 9 sample 0  [0 -1 1]\n",
      "epoch 9 sample 1  [0 -1 0]\n",
      "epoch 9 sample 2  [1 -1 1]\n",
      "epoch 9 sample 3  [1 0 2]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "# 逻辑与数据\n",
    "samples_and = [\n",
    "    [0, 0, 0],\n",
    "    [1, 0, 0],\n",
    "    [0, 1, 0],\n",
    "    [1, 1, 1],\n",
    "]\n",
    "\n",
    "# 逻辑或数据\n",
    "samples_or = [\n",
    "    [0, 0, 0],\n",
    "    [1, 0, 1],\n",
    "    [0, 1, 1],\n",
    "    [1, 1, 1],\n",
    "]\n",
    "\n",
    "# 逻辑异或数据\n",
    "samples_xor = [\n",
    "    [0, 0, 0],\n",
    "    [1, 0, 1],\n",
    "    [0, 1, 1],\n",
    "    [1, 1, 0],\n",
    "]\n",
    "\n",
    "def perceptron(samples):\n",
    "    w = np.array([1, 2])\n",
    "#     w = np.zeros(2)\n",
    "    b = 0\n",
    "    a = 1\n",
    "    \n",
    "    for i in range(10):\n",
    "        for j in range(4):\n",
    "            x = np.array(samples[j][:2])\n",
    "            y = 1 if np.dot(w, x) + b > 0 else 0\n",
    "            d = np.array(samples[j][2])\n",
    "            \n",
    "            delta_b = a*(d-y)\n",
    "            delta_w = a*(d-y)*x\n",
    "            \n",
    "            print('epoch {} sample {}  [{} {} {}]'.format(\n",
    "                i, j, w[0], w[1], b\n",
    "            ))\n",
    "            \n",
    "            w = w + delta_w\n",
    "            b = b + delta_b\n",
    "            \n",
    "\n",
    "if __name__ == '__main__':\n",
    "    print('logical and')\n",
    "    perceptron(samples_and)\n",
    "    print('logical or')\n",
    "    perceptron(samples_or)\n",
    "    print('logical xor')\n",
    "    perceptron(samples_xor)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### logical and\n",
    "可以看到，最终在epoch2的样本2，3上，参数就已经停止更新了，接下来参数都完全没有更新。最终得到的参数为$w_1 = 1, w_2 = 2, b = -2$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### logical or\n",
    "可以得到$w_1 = 1, w_2 = 2, b = 0$<br>\n",
    "事实上比较巧合，我们初始化使用的参数，正好满足这里的数据。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### logical xor\n",
    "感知器算法无法得出满足这组数据的参数，这是因为异或问题属于线性不可分问题，单个感知器算法只能解决线性可分问题。<br>\n",
    "如下图所示，为异或逻辑的数据，无法找到一条直线将两类数据完全分割。\n",
    "\n",
    "<img style=\"float: left;\" src=\"xor.png\" width=\"40%\">"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
